<?php

/**
  *
  * Module Library - simplifies management and loading of shard data
  *  
  * @version		  0.1
  * @last_update September 27, 2007 
  * @package		  CodeIgniter
  * @subpackage	Module Library
  * @category    Presentation
  * @author		  Tim Wood [codearachnid]
  * @link		    http://code.codearachnid.com
  *
  * Folder & File structure:
  * /application
  *   /modules
  *     /[module_name]
  *       |config.php
  *       |shard.php
  *       |model.php (optional)
  *       |[helper_name].php (optional)
  *       |[style_name].css (optional)
  *       |[script_name].js (optional)
  *
  */

class Module {
	
	var $CI;
	var $module_prefix = 'module_';
	var $module_folder = 'modules';
	var $modules_loaded = array();

	function Module() {
		$this->CI =& get_instance();
		$this->CI->lang->load('module');
	}
	
	function set_config($load_module) {
    $file = APPPATH.$this->module_folder.'/'.$load_module.'/config'.EXT;
    if (file_exists($file)) {
      include($file);
      $this->module_prefix = (isset($config['module_prefix'])) ? $config['module_prefix'] : $this->module_prefix;
      $this->module_folder = (isset($config['module_folder'])) ? $config['module_folder'] : $this->module_folder;
      $this->CI->config->set_item($this->module_prefix.$load_module, $config);
    }
  }
  
  function set_model($load_module, $name = '', $db_conn = FALSE) {
		if ($this->isempty($load_module)) return;
		if ($this->isempty($name)) $name = $this->module_prefix.$load_module;
		if (in_array($name, $this->CI->load->_ci_models, TRUE)) return;

		if (isset($this->CI->load->$name)) {
			show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
		}
	
		$load_module = strtolower($load_module);
		
		if ( ! file_exists(APPPATH.$this->module_folder.'/'.$load_module.'/model'.EXT)) {
			show_error('Unable to locate the model you have specified: '.$load_module);
		}
				
		if ($db_conn !== FALSE AND ! class_exists('CI_DB')) {
			if ($db_conn === TRUE) $db_conn = '';
			$this->CI->load->database($db_conn, FALSE, TRUE);
		}
	
		if ( ! class_exists('Model')) require_once(BASEPATH.'libraries/Model'.EXT);

		require_once(APPPATH.$this->module_folder.'/'.$load_module.'/model'.EXT);

		$load_module = ucfirst($this->module_prefix.$load_module);
				
		$this->CI->load->$name = new $load_module();
		$this->CI->load->$name->_assign_libraries();
		
		$this->CI->load->_ci_models[] = $name;    
  }

  function render($load_module, $method = TRUE) {
    if(!$this->isempty($load_module)) {
      if (file_exists(APPPATH.$this->module_folder.'/'.$load_module)) {
        if(!in_array($load_module, $this->modules_loaded)) {
          $this->set_config($load_module);
          $config = $this->CI->config->item($this->module_prefix.$load_module);
          $this->set_model($load_module,@$config['model_name']);
        }
        $this->modules_loaded[] = $load_module;
        if($method) {
          return $this->CI->load->_ci_load(array('path' => APPPATH.$this->module_folder.'/'.$load_module.'/shard'.EXT, 'vars' => $this->CI->load->_ci_object_to_array($this->CI->config->item($this->module_prefix.$load_module)), 'return' => TRUE));
        } else {
          echo $this->CI->load->_ci_load(array('path' => APPPATH.$this->module_folder.'/'.$load_module.'/shard'.EXT, 'vars' => $this->CI->load->_ci_object_to_array($this->CI->config->item($this->module_prefix.$load_module)), 'return' => TRUE));
        }
      } else {
        $err = $this->CI->lang->line('error_missing_module',$load_module);
        if($method) {
          return $err;
        } else {
          echo $err;
        }
      }
    } else {
      $err = $this->CI->lang->line('error_blank_module',$load_module);
      if($method) {
        return $err;
      } else {
        echo $err;
      }
    }
  }

	function isempty($var) {
	  if (((is_null($var) || rtrim($var) == "") && $var !== false) || (is_array($var) && empty($var))) {
	    return true;
	  } else {
	    return false;
	  }
	} 
	
}

?>
